<?php
$ROOT_DIR_PATH = '';

include_once($ROOT_DIR_PATH . 'include/config.php'); //Incldes

$listingUrl = "listing.php"; //Redirect URl	  
//Variable initilazation
$cus_id = "";
$cat_id = "";
$catref_id = "";
$firstname = "";
$lastname = "";
$email = "";
$category = "";
$catref_cus_id = "";
$catref_cat_id = "";
$errorArray = Array();
$succMessage = "";
$requestedData = $_POST;
$getData = $_GET;
$mode = "add";


$selectsql = "SELECT * FROM `tbl_category`";
$categorydata = $DBOBJ->select($selectsql);



if ($_GET) {

    if (array_key_exists('cus_id', $_GET)) {
        //Edit select operations
        $cus_id = $_GET['cus_id'];
        $mode = "edit";
        $selectSql = "SELECT * FROM `tbl_customer` 
			WHERE cus_id = '" . $cus_id . "'";
        $userdata = $DBOBJ->select($selectSql);

        $selectcatrefsql = "SELECT * FROM `tbl_cat_ref` where catref_cus_id = '" . $cus_id . "'";
        $categoryrefdata = $DBOBJ->select($selectcatrefsql);

        foreach ($categoryrefdata as $key => $value) {
            $finalCategory[$value["catref_cat_id"]] = $value;
        }
        if ($userdata) {
            $firstname = $userdata[0]['cus_firstname'];
            $lastname = $userdata[0]['cus_lastname'];
            $email = $userdata[0]['cus_email'];
        }
    }
}
if ($requestedData) {
    //Hideen Varible logic
    $cus_id = $requestedData['cus_id'];
    if ($cus_id != "") {
        $mode = 'edit';
    }

    $firstname = $requestedData['firstname'];
    $lastname = $requestedData['lastname'];
    $email = $requestedData['email'];
    $category = $requestedData['category'];

    if ($firstname == "") {
        $errorArray[] = "Please enter firstname";
    }

    if ($lastname == "") {
        $errorArray[] = "Please enter lastname";
    }

    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
        $errorArray[] = "Please enter valid email";
    }

    if (!array_key_exists('category', $requestedData)) {
        $errorArray[] = "Please select category";
    }

    //insert logic
    if (count($errorArray) == 0) {
        if ($mode == 'add') {
            $insertcustomer = "INSERT INTO `tbl_customer`(
						`cus_id`,
						`cus_firstname`,
						`cus_lastname`,
						`cus_email`)
						VALUES (
						NULL,
						'" . $firstname . "',
						'" . $lastname . "',
						'" . $email . "'
						)";

            $cus_id = $DBOBJ->insert($insertcustomer);

            if ($cus_id > 0) {
                if (array_key_exists('category', $requestedData)) {
                    foreach ($category as $key => $value) {
                        $insertcategory = "INSERT INTO `tbl_cat_ref`(
								`catref_id`,
								`catref_cus_id`,
								`catref_cat_id`)
								VALUES(
								NULL,
								'" . $cus_id . "',
								'" . $value . "')";
                        $cat_id = $DBOBJ->insert($insertcategory);
                    }
                }
            }
            if ($catref_id > 0) {
                $GENOBJ->redirect($listingUrl);
            } else {
                $errorArray[] = "Error in insertion process";
            }
        } 
        else {
            //update logic
            $updatecustomer = "UPDATE tbl_customer SET 
						cus_firstname = '" . $firstname . "',
						cus_lastname = '" . $lastname . "',
						cus_email = '" . $email . "'
						WHERE cus_id = '" . $cus_id . "'";

            $DBOBJ->execute($updatecustomer);

            if ($cus_id != '') {
                $deletecategorySql = "delete from tbl_cat_ref where catref_cus_id = '" . $cus_id . "'";
                $DBOBJ->execute($deletecategorySql);
                //Redirect
                $GENOBJ->redirect($listingUrl);
            }

            if (array_key_exists('category', $requestedData)) {
                foreach ($category as $key => $value) {
                    $insertcategory = "INSERT INTO `tbl_cat_ref`(
							`catref_id`,
							`catref_cus_id`,
							`catref_cat_id`)
							VALUES(
							NULL,
							'" . $cus_id . "',
							'" . $value . "')";
                    $cat_id = $DBOBJ->insert($insertcategory);
                }
            }
            $GENOBJ->redirect($listingUrl);
        }
    }
}
?>
<?php include_once($ROOT_DIR_PATH . 'template/header.php'); ?>	
<h2>
<?php echo ($mode == 'add') ? "Add Customer" : "Edit customer" ?>
</h2>
<br/>

<a href="cus_listing.php">BACK TO LISTING</a>	
<br/>
<br/>

<?php if ($succMessage != ""): ?>
    <h4><div style="color:green;"><?php echo $succMessage; ?></div></h4>
<?php endif; ?>
<?php if (count($errorArray) > 0): ?>	
    <?php foreach ($errorArray as $_error): ?>
        <div style="color:red;"><?php echo $_error; ?></div>
    <?php endforeach; ?>
<?php endif; ?>

<form action="" method="POST" name="myForm" id="myForm">

    <label for="firstname">FirstName :</label>
    <input id="firstname" type="text" name="firstname" value="<?php echo $firstname; ?>" />
    <br/>
    <br/>

    <label for="lastname">Lastname : </label>
    <input id="lastname" type="text" name="lastname" value="<?php echo $lastname; ?>" />
    <br/>
    <br/>

    <label for="email">Email :</label>
    <input id="email" type="text" name="email" value="<?php echo $email; ?>" />
    <br/>
    <br/>

<?php foreach ($categorydata as $key => $value): ?>	
    <?php
    $checked = "";
    if ($mode == 'edit') {
        if (array_key_exists($value['cat_id'], $finalCategory)) {
            $checked = "checked";
        }
    }
    ?>

        <input id="category_<?php echo $value["cat_id"] ?>" type="checkbox"  <?php echo $checked; ?> name="category[]" value="<?php echo $value["cat_id"] ?>">
        <label for="category_<?php echo $value["cat_id"] ?>"><?php echo $value["cat_name"] ?></label>
    <?php endforeach; ?>
    <br/>
    <br/>

    <input type="submit" name="Submit" value="<?php echo ($mode == 'add') ? "Add Customer" : "Edit Customer" ?>"/>
    <input id="cus_id" type="hidden" name="cus_id" value="<?php echo $cus_id; ?>" />
</form>
<br/>
<br/>

<a href="cus_listing.php">BACK TO LISTING</a>

<?php include_once($ROOT_DIR_PATH . 'template/footer.php'); ?>